<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Tool
 *
 * @author Implement <hdjahs@qq.com>
 */
class Tool {

    private static $ins = null;

    final private function __construct() {
        
    }

    final private function __clone() {
        
    }

    public static function getIns() {
        if (self::$ins == null) {
            self::$ins = new self();
        }
        return self::$ins;
    }

    public function p($value, $is_end = '') {
        echo "<pre>";
        print_r($value);
        echo "</pre>";
        echo "<br />";
        if (!empty($is_end)) {
            die();
        }
    }

    public function strCutByStr(&$str, $findStart, $findEnd = false, $encoding = 'utf-8') {
        if (is_array($findStart)) {
            if (count($findStart) === count($findEnd)) {
                foreach ($findStart as $k => $v) {
                    if (($result = strCutByStr($str, $v, $findEnd[$k], $encoding)) !== false) {
                        return $result;
                    }
                }
                return false;
            } else {
                return false;
            }
        }

        if (($start = mb_strpos($str, $findStart, 0, $encoding)) === false) {
            return false;
        }

        $start += mb_strlen($findStart, $encoding);

        if ($findEnd === false) {
            return mb_substr($str, $start, NULL, $encoding);
        }

        if (($length = mb_strpos($str, $findEnd, $start, $encoding)) === false) {
            return false;
        }

        return mb_substr($str, $start, $length - $start, $encoding);
    }

    public function downloadImage($url, $filename = "", $path = "") {

        if ($path == "") {
            return false;
        }
        if ($filename == "") {
            $ext = strrchr($url, ".");
            if ($ext != ".gif" && $ext != ".jpg"):return false;
            endif;
            $filename = $path . date("dMYHis") . $ext;
        }

        ob_start();
        readfile($url);
        $img = ob_get_contents();
        ob_end_clean();
        $size = strlen($img);


        $fp2 = @fopen($path . $filename, "a");
        fwrite($fp2, $img);
        fclose($fp2);

        return $filename;
    }

    public function http_get_img($url) {
        $ch = curl_init();
        $headers = array();
        $headers[] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
        $headers[] = 'Cookie:BAIDUID=727DB9D44C064DBFD4C7F6452D08FBE6:FG=1; PSTM=1476249121; BIDUPSID=04E61396FB2E5AFCD30B6AD92CEB3469; pgv_pvi=2564306944';
        $headers[] = 'Host:d.hiphotos.baidu.com';
        $headers[] = 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }

        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public function createPath($path) {
        if (!file_exists($path)) {
            mkdir($path);
        }
    }

    public function httpGet($url) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 要求结果为字符串且输出到屏幕上

        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }

        $output = curl_exec($ch);  //执行
        curl_close($ch);  //关闭句柄
        return $output;
    }

}
